import {Injectable} from '@angular/core';
import {Http} from "@angular/http";
import {Observable} from "rxjs/Observable";

@Injectable()
export class StockService {

    constructor(public http: Http) {
    }

    private stocks: Stock[] = [
        new Stock(1, "第一只股票", 1.99, 3.5, "只是第一只股票", ["IT", "互联网"]),
        new Stock(2, "第二只股票", 2.99, 1.5, "只是第一只股票", ["IT"]),
        new Stock(3, "第三只股票", 3.99, 3.5, "只是第一只股票", ["金融"]),
        new Stock(4, "第四只股票", 4.99, 4.5, "只是第一只股票", ["IT", "互联网"])
    ];

    getStocks(): Observable<Stock[]> {
        return this.http.get('/api/stocks')
            .map(response => response.json());
    }

    getStock(id: number): Observable<Stock> {
        return this.http.get('/api/stock/' + id)
            .map(res => res.json());

        // var stockId = id;
        // var stock = this.getStocks().find(stock => stock.id == stockId);
        // if (!stock) {
        //     stock = new Stock(0, "", 0, 0, "", []);
        // }
        // return stock;
    }
}

export class Stock {
    constructor(public id: number,
                public name: string,
                public price: number,
                public rating: number,
                public desc: string,
                public categories: Array<string>) {

    }
}